
package processors;

import containers.Groups;
import containers.TestsTaken;
import java.util.LinkedList;
import readers.QueryStringHandler;
import readers.UserReader;

/**
 * Bad name class choice, sorry this changed with time
 * This class now handles processing of all quizzes
 * @author Chris
 */
public class processorAvailableQuizzes extends processor {
    private String _errMsg;
    processorAvailableQuizzes(String query) {
        super(query);
        _errMsg = "[ERROR]";
    }
    
    @Override
    String process() {
        QueryStringHandler qsh = new QueryStringHandler(_queryString);
        String userName = qsh.getElement("username");
        
        if(userName == null) {
            return _errMsg;
        }
        LinkedList<Groups> grps = new LinkedList<Groups>();
        LinkedList<String> tests = new LinkedList<String>();
        LinkedList<TestsTaken> taken = new LinkedList<TestsTaken>();
        //Basically a place to store the final list of all the tests combined
        //with their test scores
        LinkedList<TestsTaken> finalList = new LinkedList<TestsTaken>();
        
        //This will get all available groups to the student
        if(!UserReader.getStudentsGroups(userName, grps)) {
            grps.clear();
            return _errMsg;
        }
        
        Groups grp;
        while(!grps.isEmpty()) {
            grp = grps.getFirst();
            
            //Will keep adding valid test names until all are listed
            UserReader.getTests(grp.teacherName, grp.groupName, tests);
            while(!tests.isEmpty()) {
                finalList.add(new TestsTaken(grp.teacherName, grp.groupName,
                    tests.getFirst(), "-1"));
                tests.removeFirst();
            }
            
            grps.removeFirst();
        }
        
        UserReader.getTakenTests(userName, taken);
        
        TestsTaken tt;
        int index;
        while(!taken.isEmpty()) {
           tt = taken.getFirst();
           
           index = finalList.indexOf(tt);
           if(index == -1) {
               finalList.add(tt);
               taken.removeFirst();
               continue;
           }
           
           //Basically overwrite the -1 score placed in earlier
           finalList.get(index).score = tt.score;
           
           taken.removeFirst();
        }
        
        //Use to be in xml form but easy to process in space deliminated
        String result = "";//<quizList>";
        
        while(!finalList.isEmpty()) {
            tt = finalList.getFirst();
            
            result += tt.teacher + "_" + 
                      tt.group + " " +
                      tt.test + " " +
                      "0\n";//0 is the expiry date of this quiz
                    //"<quiz><teacher>" + tt.teacher + "</teacher>" +
                    //"<group>" + tt.group + "</group>" +
                    //"<test>" + tt.test + "</group>" +
                    //"<score>" + tt.score + "/score>" +
                    //"<expires>0</expires></quiz>";
            
            finalList.removeLast();
        }
        result += "";//"</quizList>";
        
        return result;
    }
}
